import app_network
import app_mqtt
import _thread
import time
from machine import Pin


# RTU一共4个可控指示灯
led_4G = Pin(38, Pin.OUT)  # GPIO38 4G LED
led_wifi = Pin(39, Pin.OUT) # GPIO39 wifi LED
led_eth = Pin(40, Pin.OUT) # GPIO40 以太网 LED
led_sta = Pin(21, Pin.OUT) # GPIO21 运行 LED
led_4G.value(0)
led_wifi.value(0)
led_eth.value(0)
led_sta.value(0)
frequency = 1
n = 0
def net_led_disp(id):
    global n
    while True:
        net1_connect_flag=app_network.get4gFlag()
        net2_connect_flag=app_network.getethFlag()
        net3_connect_flag=app_network.getwifiFlag()
        n+=1
        #led_sta.value(n%2)
        if net1_connect_flag:
            if app_mqtt.mqtt_get_server_state():
                led_4G.value(1)
            else:
                led_4G.value(n%2)
        if net2_connect_flag:
            if app_mqtt.mqtt_get_server_state():
                led_eth.value(1)
            else:
                led_eth.value(n%2)
        if net3_connect_flag:
            if app_mqtt.mqtt_get_server_state():
                led_wifi.value(1)
            else:
                led_wifi.value(n%2)
        time.sleep_ms(500)
        if n>99:
            n=0
# 生成一个函数，让LED闪烁，不同的频率作为参数
def led_blink(id):
    while True:
        led_sta.value(1)
        time.sleep_ms(1000/frequency)
        led_sta.value(0)
        time.sleep_ms(1000/frequency)
# 开启4G指示灯线程
def led_4G_thread():
    _thread.start_new_thread(led_blink,(1,))
#开启状态灯线程
def led_disp_thread():
    _thread.start_new_thread(net_led_disp,(2,))


